package com.redis.zset;

import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.ZParams;

/**
 * 有序集合
 */
public class RedisZSet {
	public static void main(String[] args) {
		interAndUnion();
	}
	
	public static void interAndUnion() {
		JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");
		Jedis jedis = jedisPool.getResource();
		
		// 大家对游戏客户端的贡献分别是: 0.15 0.3 0.15 0.15 0.25
		jedis.zadd("game client", 0.15, "张三");
		jedis.zadd("game client", 0.3, "王五");
		jedis.zadd("game client", 0.15, "赵六");
		jedis.zadd("game client", 0.15, "小丽");
		jedis.zadd("game client", 0.25, "小翠");
		// 大家对游戏服务器的贡献分别是: 0.15 0.3 0.15 0.15 0.25
		jedis.zadd("game server", 0.15, "小明");
		jedis.zadd("game server", 0.3, "王五");
		jedis.zadd("game server", 0.15, "小刚");
		jedis.zadd("game server", 0.15, "小红");
		jedis.zadd("game server", 0.25, "小兰");
		
		// 客户端贡献排名
		print(jedis, "game client");
		// 服务器贡献排名
		print(jedis, "game server");

		ZParams params = new ZParams();
		params.weightsByDouble(0.5, 0.5);
		// 交集(对游戏贡献大的排名)
		jedis.zinterstore("top", params, "game server", "game client");
		print(jedis, "top");
		
		// 并集(大家对游戏的贡献排名)
		jedis.zunionstore("all", params, "game server", "game client");
		print(jedis, "all");
		
		jedis.close();
		jedisPool.close();
		jedisPool.destroy();
	}
	
	// 输出带权重的集合
	public static void print(Jedis jedis, String key) {
		Set<Tuple> set = jedis.zrangeWithScores(key, 0, -1);
		System.out.print("[ ");
		for (Tuple tuple : set) {
			System.out.print(tuple.getElement() + ":" + tuple.getScore() + " ");
		}
		System.out.println("]");
	}
	
	public static void rank() {
		JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");
		Jedis jedis = jedisPool.getResource();
		
		print(jedis, "qinshou");
		// 查看排名
		System.out.println(jedis.zrank("qinshou", "游戏服务器"));
		
		jedis.close();
		jedisPool.close();
		jedisPool.destroy();
	}
	
	public static void score() {
		JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");
		Jedis jedis = jedisPool.getResource();
		
		print(jedis, "qinshou");
		// 查看某个元素的权重
		System.out.println(jedis.zscore("qinshou", "游戏服务器"));
		
		jedis.close();
		jedisPool.close();
		jedisPool.destroy();
	}
	
	public static void count() {
		JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");
		Jedis jedis = jedisPool.getResource();
		
		print(jedis, "qinshou");
		// 集合元素个数
		System.out.println(jedis.zcard("qinshou"));
		// 权重50-80,包含50-80, 一共多少个
		System.out.println(jedis.zcount("qinshou", 50, 80));
		
		jedis.close();
		jedisPool.close();
		jedisPool.destroy();
	}
	
	public static void delete() {
		JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");
		Jedis jedis = jedisPool.getResource();
		
		System.out.println(jedis.zrange("qinshou", 0, -1));
		// 删除集合中的一个元素
		jedis.zrem("qinshou", "游戏前端");
		System.out.println(jedis.zrange("qinshou", 0, -1));
		
		jedis.close();
		jedisPool.close();
		jedisPool.destroy();
	}
	
	public static void add() {
		JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");
		Jedis jedis = jedisPool.getResource();
		
		// 添加一个元素
		jedis.zadd("qinshou", 100, "游戏前端");
		jedis.zadd("qinshou", 90, "游戏服务器");
		jedis.zadd("qinshou", 90, "游戏美术");
		jedis.zadd("qinshou", 80, "游戏策划");
		jedis.zadd("qinshou", 70, "游戏渠道接入");
		jedis.zadd("qinshou", 60, "游戏运营");
		jedis.zadd("qinshou", 50, "游戏商务");
		jedis.zadd("qinshou", 40, "游戏测试");
		
		System.out.println(jedis.zrange("qinshou", 0, -1));
		
		jedis.close();
		jedisPool.close();
		jedisPool.destroy();
	}
	
}
